<%@ page import="java.util.List" %>
<%@ page import="dao.WarehouseDao" %>
<%@ page import="dao.impl.WarehouseImpl" %>
<%@ page import="po.Warehouse" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="resources/css/amazeui.min.css"/>
    <link rel="stylesheet" href="resources/css/admin.css"/>

    <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    <script>
        const BOM = "\ufeff";

        function convertToCSV(tableId) {
            let csv = [];
            $("#" + tableId).find("tr").each(function () {
                let row = [];
                $(this).find("td:not(:last-child),th:not(:last-child)").each(function () {
                    let val = $(this).text();
                    if (/[,\"\n]/.test(val)) {
                        val = "\"" + val.replace(/\"/g, "\"\"") + "\"";
                    }
                    row.push(val);
                });
                csv.push(row.join(","));
            });
            return csv.join("\n");
        }

        function downloadCSV(csv) {
            const blob = new Blob([BOM + csv], {type: "text/csv;charset=utf-8"});
            const link = document.createElement("a");
            link.href = URL.createObjectURL(blob);
            link.download = "data.csv";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            URL.revokeObjectURL(link.href);
        }

        function exportTableToCSV(tableId) {
            let csv = convertToCSV(tableId);
            downloadCSV(csv);
        }

        function convertToExcel(tableId) {
            let excel = "";
            $("#" + tableId).find("tr").each(() => {
                excel += "<tr>";
                $(this).find("td:not(:last-child),th:not(:last-child)").each(() => {
                    let val = $(this).text().replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
                    excel += `<td>${val}</td>`;
                });
                excel += "</tr>";
            });
            return `<table>${excel}</table>`;
        }

        function downloadExcel(excel) {
            const blob = new Blob([BOM + excel], {type: "application/vnd.ms-excel;charset=utf-8"});
            const link = document.createElement("a");
            link.href = URL.createObjectURL(blob);
            link.download = "data.xls";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            URL.revokeObjectURL(link.href);
        }

        function exportTableToExcel(tableId) {
            let excel = convertToExcel(tableId);
            downloadExcel(excel);
        }

        function exportTable(tableId) {
            let format = $("#sel").val();
            format === "csv" ? exportTableToCSV(tableId) : exportTableToExcel(tableId);
        }
    </script>
    <style>
        #sel, #bt{
            background-color: #4CAF50;
            border: none;
            color: white;
            width: 50px;
            height: 40px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
        }
    </style>
</head>
<%
    String warehouseId = request.getParameter("warehouseId");
    String goodName = request.getParameter("goodName");
    StringBuilder sql = new StringBuilder("select * from warehouse where 1=1 ");
    if (warehouseId != null) {
        sql.append("and warehouse_id=" + "'").append(warehouseId).append("'");
    }
    if (goodName != null) {
        sql.append("and good_name like" + "'").append("%").append(goodName).append("%").append("'");
    }
    WarehouseDao warehouseDao = new WarehouseImpl();
    List<Warehouse> warehouses = warehouseDao.selectWarehouse(String.valueOf(sql));

%>
<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">仓库物品管理</strong><small></small></div>
    </div>
    <hr>
    <div class="am-g">
        <div class="am-u-sm-12 am-u-md-3">
        </div>
        <div class="am-u-sm-12 am-u-md-3">
            <div class="am-input-group am-input-group-sm">
        <span class="am-input-group-btn">
          <form action="warehouse.jsp?warehouseId=<%=warehouseId%>" method="post">
            <label>
              <input type="text" name="goodName" class="am-form-field" style="width: 100px; height: 36px;">
              <button class="am-btn am-btn-default" type="submit">搜索</button>
            </label>
          </form>
        </span>
            </div>
        </div>
    </div>
    <div class="am-g">
        <div class="am-u-sm-12">
            <form class="am-form">
                <table class="am-table am-table-striped am-table-hover table-main" id="table1">
                    <thead>
                    <tr>
                        <th class="table-id">仓库编号</th>
                        <th class="table-id">货物编号</th>
                        <th class="table-title">货物名称</th>
                        <th class="table-title">货物类型</th>
                        <th class="table-title">货物数量</th>
                        <th class="table-title">货物价格</th>
                        <th class="table-title">进库日期</th>
                        <th class="table-set">操作货物</th>
                    </tr>
                    </thead>

                    <%
                        for (Warehouse w : warehouses) {
                    %>
                    <tbody>
                    <tr>
                        <td><%=w.getWarehouseId()%>
                        </td>
                        <td><%=w.getGoodId()%>
                        </td>
                        <td>
                            <a href="#"><%=w.getGoodName()%>
                            </a>
                        </td>
                        <td>
                            <a href="#"><%=w.getType()%>
                            </a>
                        </td>
                        <td>
                            <a href="#"><%=w.getCount() %>
                            </a>
                        </td>
                        <td>
                            <a href="#"><%=w.getCost() %>
                            </a>
                        </td>
                        <td>
                            <a href="#"><%=w.getImStorage() %>
                            </a>
                        </td>
                        <td>
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button class="am-btn am-btn-default am-btn-xs am-text-secondary">
                                        <a href="updateGood.jsp?goodid=<%=w.getGoodId()%>">修改</a>
                                    </button>
                                    <button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                        <a href="exWarehouse.jsp?goodid=<%=w.getGoodId()%>&warehouseId=<%=w.getWarehouseId()%>">出库</a>
                                    </button>
                                </div>
                            </div>
                        </td>
                    </tr>
                    <%}%>
                    </tbody>
                </table>
                <select id="sel">
                    <option value="csv">CSV</option>
                    <option value="excel">excel</option>
                </select>
                <button id="bt" onclick="exportTable('table1')">导出</button>
                <hr>
            </form>
        </div>
    </div>
</div>
</body>
</html>
